package com.lyfei.yd.admin.web;

import com.lyfei.yd.common.core.domain.AjaxResult;
import com.lyfei.yd.system.domain.entity.Banner;
import com.lyfei.yd.system.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @GetMapping("/page")
    public AjaxResult page(@RequestParam(defaultValue = "1") Integer current,
                           @RequestParam(defaultValue = "10") Integer pageSize) {

        // 查询逻辑
        return AjaxResult.success(bannerService.page(current, pageSize));
    }

    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody Banner banner) {
        bannerService.saveOrUpdate(banner);
        return AjaxResult.success();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        Banner banner = bannerService.getById(id);
        if (banner == null) {
            return AjaxResult.error("删除失败，数据不存在");
        } else {
            bannerService.removeById(id);
            return AjaxResult.success();
        }
    }

    @GetMapping("/detail/{id}")
    public AjaxResult getBannerDetail(@PathVariable Long id) {
        return AjaxResult.success(bannerService.getBannerDetail(id));
    }
}
